package com.example.note.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.note.bean.Note;
import com.example.note.utils.DBUtils;

import java.util.ArrayList;
import java.util.List;

public class MyHelper extends SQLiteOpenHelper {
    private final SQLiteDatabase sqLiteDatabase;//该对象用于数据库的增删改查

    public MyHelper(Context context) {
        super(context, DBUtils.DATABASE_NAME, null, DBUtils.DATABASE_VERSION);
        sqLiteDatabase = this.getWritableDatabase();//获得一个可写的数据库对象
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        //创建表
        sqLiteDatabase.execSQL(DBUtils.sql);//执行定义好的sql语句
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        //更新数据库
    }

    //添加数据
    public boolean insertData(String userContent, String userTime) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBUtils.NOTEPAD_CONTENT, userContent);
        contentValues.put(DBUtils.NOTEPAD_TIME, userTime);
        return sqLiteDatabase.insert(DBUtils.DATABASE_TABLE, null, contentValues) > 0;
    }

    //删除数据
    public boolean deleteData(int id) {
        String sql = DBUtils.NOTEPAD_ID + "=?"; //相当于条件语句 id=?
        String[] contentValuesArray = new String[]{String.valueOf(id)};
        return sqLiteDatabase.delete(DBUtils.DATABASE_TABLE, sql, contentValuesArray) > 0;
    }

    //修改数据
    public boolean updateData(int id, String content, String userTime) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBUtils.NOTEPAD_CONTENT, content);
        contentValues.put(DBUtils.NOTEPAD_TIME, userTime);
        String sql = DBUtils.NOTEPAD_ID + "=?";
        String[] string = new String[]{String.valueOf(id)};
        return sqLiteDatabase.update(DBUtils.DATABASE_TABLE, contentValues, sql, string) > 0;
    }

    //查询数据
    public List<Note> queryData() {
        List<Note> list = new ArrayList<Note>();
        Cursor cursor = sqLiteDatabase.query(DBUtils.DATABASE_TABLE, null, null,
                null, null, null, DBUtils.NOTEPAD_ID + " desc");
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Note noteInfo = new Note();
                @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex(DBUtils.NOTEPAD_ID));
                @SuppressLint("Range") String content = cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_CONTENT));
                @SuppressLint("Range") String time = cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_TIME));
                noteInfo.setId(id);
                noteInfo.setNotepadContent(content);
                noteInfo.setNotepadTime(time);
                list.add(noteInfo);
            }
            cursor.close();
        }
        return list;
    }
}
